package edgeEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Number"
)

type DeviceValueExEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	Manufacturer string // 设备厂商名称
	DeviceType   string // 设备类型名
	DeviceName   string // 设备名称

	Params map[string]*DeviceObjectValue // 配置参数
}

type deviceValueExPo struct {
	Id           int64                  `json:"id"`           // 对象id
	CreateTime   int64                  `json:"createTime"`   // 创建时间
	UpdateTime   int64                  `json:"updateTime"`   // 更新时间
	Manufacturer string                 `json:"manufacturer"` // 设备厂商名称
	DeviceType   string                 `json:"deviceType"`   // 设备类型名
	DeviceName   string                 `json:"deviceName"`   // 设备名称
	Params       map[string]interface{} `json:"params"`       // 配置参数
}

func (e *DeviceValueExEntity) Type() string {
	return "DeviceValueExEntity"
}

func (e *DeviceValueExEntity) TableName() string {
	return ""
}

func (e *DeviceValueExEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *DeviceValueExEntity) Instance() *DeviceValueExEntity {
	return &DeviceValueExEntity{
		BaseEntity: new(commEntity.BaseEntity),
		Params:     make(map[string]*DeviceObjectValue),
	}
}

func (e *DeviceValueExEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *DeviceValueExEntity) NewPo() interface{} {
	return &deviceValueExPo{
		Params: make(map[string]interface{}),
	}
}

func (e *DeviceValueExEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *DeviceValueExEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.DeviceName)
	return p
}

func (e *DeviceValueExEntity) BuildByJson(jsn *string) error {
	var po deviceValueExPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *DeviceValueExEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *DeviceValueExEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*deviceValueExPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.Manufacturer = po.Manufacturer
	ptr.DeviceType = po.DeviceType
	ptr.DeviceName = po.DeviceName

	for key, val := range po.Params {
		value := val.(map[string]interface{})

		objVal := &DeviceObjectValue{}
		objVal.Value = value["value"]
		objVal.Time = Number.MakeInt64(value["time"])
		ptr.Params[key] = objVal
	}

}

func (e *DeviceValueExEntity) BuildToPo() interface{} {
	ptr := e
	po := deviceValueExPo{}
	po.Params = make(map[string]interface{})

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.Manufacturer = ptr.Manufacturer
	po.DeviceType = ptr.DeviceType
	po.DeviceName = ptr.DeviceName

	for key, val := range ptr.Params {
		value := make(map[string]interface{})
		value["value"] = val.Value
		value["time"] = val.Time

		po.Params[key] = value
	}

	return &po
}
